package com.example.administrator.fastdream.date.presenter;

import android.app.Activity;
import android.content.Intent;
import android.widget.Toast;

import com.example.administrator.fastdream.date.bean.BaseBean;
import com.example.administrator.fastdream.date.bean.LoginBean;
import com.example.administrator.fastdream.date.http.JsonCallback;
import com.example.administrator.fastdream.date.utils.MyUrl;
import com.example.administrator.fastdream.view.activity.LoginActivity;
import com.lzy.okgo.OkGo;
import com.lzy.okgo.model.HttpParams;
import com.lzy.okgo.model.Response;

public class LoginPresenter {
    private Activity mActivity;
    private LoginCallBack loginCallBack;

    public LoginPresenter(Activity activity, LoginCallBack loginCallBack) {
        this.mActivity = activity;
        this.loginCallBack = loginCallBack;
    }

    public void login(String mobile, String code) {
        HttpParams params = new HttpParams();
        params.put("mobile", mobile);
        params.put("code", code);
        OkGo.<LoginBean>post(MyUrl.BASEURL + "Home/register_login")
                .tag(this)
                .params(params)
                .execute(new JsonCallback<LoginBean>(LoginBean.class) {
                    @Override
                    public void onSuccess(Response<LoginBean> response) {
                        if (response.body() == null) {
                            return;
                        }
                        String status = response.body().getStatus();
                        if (status.equals("1")) {
                            loginCallBack.login(response.body());
                        } else if (status.equals("2")) {
                            Toast.makeText(mActivity, response.body().getMessage(), Toast.LENGTH_SHORT).show();
                        } else {
                            mActivity.startActivity(new Intent(mActivity, LoginActivity.class));
                        }
                    }
                });
    }

    public void getCode(String mobile) {
        HttpParams params = new HttpParams();
        params.put("mobile", mobile);
        OkGo.<BaseBean>post(MyUrl.BASEURL + "Home/get_code")
                .tag(this)
                .params(params)
                .execute(new JsonCallback<BaseBean>(BaseBean.class) {
                    @Override
                    public void onSuccess(Response<BaseBean> response) {
                        if (response.body() == null) {
                            return;
                        }
                        String status = response.body().getStatus();
                        if (status.equals("1")) {

                        } else if (status.equals("2")) {
                            Toast.makeText(mActivity, response.body().getMessage(), Toast.LENGTH_SHORT).show();
                        } else {
                            mActivity.startActivity(new Intent(mActivity, LoginActivity.class));
                        }
                    }
                });
    }

    public interface LoginCallBack {
        void login(LoginBean loginBean);
    }
}
